class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        unordered_map<int,int> hash;
        int res;
        for(int i = 0; i < nums.size(); i++)
        {
            if(hash[nums[i]] == 0)
            {
                int left = hash[nums[i]-1];
                int right = hash[nums[i]+1];
                hash[nums[i]] = 1 + left + right;
                hash[nums[i]-left] = 1 + left + right;
                hash[nums[i]+right] = 1 + left + right;
                res = max(res, 1 + left + right);
            }
        }

        return res;
    }
};

// 超时
class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        map<int,int> hash;
        int max = INT_MIN;
        int min = INT_MAX;

        for(int i = 0; i < nums.size(); i++)
        {
            if(nums[i] > max)
                max = nums[i];
            if(nums[i] < min)
                min = nums[i];
            
            if(hash.count(nums[i]) <= 0)
                hash.insert(pair<int,int>(nums[i], 1));
        }

        int count = 0;
        int max_count = 0;
        int first_flag = 0;
        for(int i = min; i <= max; i++)
        {
            if(hash.count(i) > 0 && first_flag == 0)
            {
                count = 1;
                first_flag = 1;
            }
            else if(hash.count(i) > 0)
                count++;
            else if(hash.count(i) <= 0 && first_flag == 1)
            {
                max_count = max_count > count ? max_count : count;
                first_flag = 0;
            }
        }

        return max_count == 0 ? count : max_count > count ? max_count : count;
    }
};
// 超时